package com.blb.service.impl;

import com.blb.entity.*;
import com.blb.mapper.ComtypeMapper;
import com.blb.mapper.MenuExtMapper;
import com.blb.service.IProductTypeService;
import com.blb.utils.LayuiPage;
import com.blb.utils.NowUserUtils;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
public class ProductTypeServiceImpl implements IProductTypeService {

    @Autowired
    private ComtypeMapper comtypeMapper;

    @Autowired
    private MenuExtMapper menuExtMapper;

    /**
     * 商品分类分页
     *
     * @param page
     * @param limit
     * @return
     * @throws IOException
     */
    @Override
    public LayuiPage page(int page, int limit, String id, String name) throws IOException {
        List<User> list = comtypeMapper.page(page, limit, id, name);

        ComtypeExample comtypeExample = new ComtypeExample();
        if (!StringUtils.isNullOrEmpty(id)) {
            comtypeExample.createCriteria().andIdLike("%" + id + "%");
        }
        if (!StringUtils.isNullOrEmpty(name)) {
            comtypeExample.createCriteria().andIdLike("%" + name + "%");
        }

        //获取总条数
        int count = (int) comtypeMapper.countByExample(comtypeExample);

        //生产json数据
        LayuiPage pagelist = new LayuiPage();
        pagelist.setData(list);
        pagelist.setCount(count);
        return pagelist;
    }

    /**
     * 查询是否有添加
     */
    @Override
    public List<Menu> bts(String menuid) {
        User user = NowUserUtils.getNowUser();
        List<Menu> list = menuExtMapper.getButtenByuserid(user.getId(), menuid);
        return list;
    }

    /**
     * 添加商品分类
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Comtype comtype) throws Exception {
        comtypeMapper.insert(comtype);
    }

    /**
     * 判断商品分类是否存在
     */
    @Override
    public List<Comtype> nameisExist(String name) throws IOException {
        ComtypeExample comtypeExample = new ComtypeExample();
        comtypeExample.createCriteria().andNameEqualTo(name);
        List<Comtype> list = comtypeMapper.selectByExample(comtypeExample);
        return list;
    }

    /**
     * 判断商品类别序号是否存在
     */
    @Override
    public List<Comtype> seqisExist(int seq) throws IOException {
        ComtypeExample comtypeExample = new ComtypeExample();
        comtypeExample.createCriteria().andSeqEqualTo(seq);
        List<Comtype> list = comtypeMapper.selectByExample(comtypeExample);
        return list;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<String> ids) throws IOException {
        ComtypeExample comtypeExample = new ComtypeExample();
        comtypeExample.createCriteria().andIdIn(ids);
        comtypeMapper.deleteByExample(comtypeExample);
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Comtype comtype) throws Exception {
        ComtypeExample comtypeExample = new ComtypeExample();
        comtypeExample.createCriteria().andIdEqualTo(comtype.getId());
        comtypeMapper.updateByExample(comtype, comtypeExample);
    }

    /**
     * 通过id 获取类别
     *
     * @param id
     * @return
     */
    @Override
    public List<Comtype> getComtypeById(String id) {
        ComtypeExample comtypeExample = new ComtypeExample();
        comtypeExample.createCriteria().andIdEqualTo(id);
        List<Comtype> comtypes = comtypeMapper.selectByExample(comtypeExample);
        return comtypes;
    }

    /**
     * 根据序号进行排序获取前九条数据
     * @return
     */
    @Override
    public List<Comtype> getComtype() {
        List<Comtype> comtype = comtypeMapper.getComtype();
        return comtype;
    }

    /**
     * 根据商品id获取分类信息
     * @return
     */
    @Override
    public List<Comtype> getComtypeByGoodsId(String ids) {
        List<Comtype> comtypeByGoodsId = comtypeMapper.getComtypeByGoodsId(ids);
        return comtypeByGoodsId;
    }

    /**
     * 根据分类名称获取分类信息
     * @param comtypeName
     * @return
     */
    @Override
    public List<Comtype> getComtypeByName(String comtypeName) {
        ComtypeExample comtypeExample = new ComtypeExample();
        comtypeExample.createCriteria().andNameEqualTo(comtypeName);
        List<Comtype> comtypes = comtypeMapper.selectByExample(comtypeExample);
        return comtypes;
    }


}
